package my.poemlib.mb;

import my.poemlib.Facade;
import my.poemlib.vo.PageControl;
import my.poemlib.vo.Poem;
import my.poemlib.vo.User;


public class PoemMB {
	
	private String queryWord;
	private String loginResult;
	
	private Poem newPoem = new Poem();
	private Poem mPoem = new Poem();
	private User user = new User();
	
	private PageControl pc = new PageControl();;
	
	private String addResult;
	
	private Long optId;
	
	public PoemMB(){
		Facade.facade.queryByWord(queryWord, pc);
	}
	
	public String showPoem(){
		if(optId!=null && optId>0){
			for(Poem p:pc.getPoemList()){
				if(p.getId() == optId){
					this.mPoem = p;
				}
			}
			return "show";
		}
		
		return "";
	}
	
	public String preAdd(){
		if(user!=null && user.getId()>0){
			return "add";
		}else{
			return "login";
		}
	}
	
	public String addPoem(){
		Facade.facade.insertPeom(newPoem);
		newPoem = new Poem();
		addResult = "添加成功";
		Facade.facade.queryByWord(queryWord, pc);
		return "addresult";
	}
	public String delPoem(){
		
		if(user!=null && user.getId()>0){
			if(optId!=null && optId>0){
				Facade.facade.delPeom(optId);
				Facade.facade.queryByWord(queryWord, pc);
			}
			return "";
		}else{
			return "login";
		}
	}
	
	public String login(){
		User u = Facade.facade.getUser(user.getUserName(), user.getPassword());
		if(u.getId()>0){
			this.user = u;
			loginResult = "登录成功！！";
		}else{
			loginResult = "登录失败！！";
		}
		return "loginresult";
	}
	public String queryPoem(){
		Facade.facade.queryByWord(queryWord, pc);
		return "";
	}
	public String getQueryWord() {
		return queryWord;
	}
	public void setQueryWord(String queryWord) {
		this.queryWord = queryWord;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public PageControl getPc() {
		return pc;
	}
	public void setPc(PageControl pc) {
		this.pc = pc;
	}
	
	public Poem getmPoem() {
		return mPoem;
	}
	public void setmPoem(Poem mPoem) {
		this.mPoem = mPoem;
	}
	public String getAddResult() {
		return addResult;
	}
	public void setAddResult(String addResult) {
		this.addResult = addResult;
	}

	public Poem getNewPoem() {
		return newPoem;
	}

	public void setNewPoem(Poem newPoem) {
		this.newPoem = newPoem;
	}
	public Long getOptId() {
		return optId;
	}
	public void setOptId(Long optId) {
		this.optId = optId;
	}

	public String getLoginResult() {
		return loginResult;
	}

	public void setLoginResult(String loginResult) {
		this.loginResult = loginResult;
	}
	
}
